<template>
  <div>
    <van-nav-bar title="黑马头条-登录" fixed placeholder />
    <van-form @submit="onSubmit">
      <!-- van-field 输入框
          name里值:是后台规定的传参用的参数名
          label前面提示文字
       -->
      <van-field
        v-model="form.mobile"
        name="mobile"
        label="手机号"
        placeholder="手机号"
            required
        :rules="[{ required: true, message: '请填写手机号',pattern:/^1[3-9]\d{9}$/ }]"
      />
      <van-field
        v-model="form.code"
        type="password"
        name="code"
        label="验证码"
        placeholder="验证码"
         required
        :rules="[{ required: true, message: '请填写验证码',pattern:/^\d{6}$/}]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
        :disabled="isLogin"
        loading-text="程序媛拼命加载中..."
        :loading ="isLogin"
          >登录</van-button
        >
      </div>
    </van-form>

  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
import { Notify } from 'vant'
export default {
  data () {
    return {
      form: {
        mobile: '16622897735',
        code: '246810'
      },
      isLogin: false
    }
  },
  methods: {
    async  onSubmit () {
      this.isLogin = true
      // console.log('submit', values)
      try {
        const res = await loginAPI({ ...this.form })
        console.log(res)
        Notify({ type: 'success', message: '登录成功' })
        setToken(res.data.data.token)
        this.isLogin = false
        this.$router.push({
          path: '/layout'
        })
      } catch (err) {
        console.error(err)
        Notify({ type: 'danger', message: '登录失败' })
        this.isLogin = false
      }
    }
  }
}
</script>

<style>
</style>
